package com.sparrow.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.UUID;

import com.sparrow.constant.*;
import com.sparrow.core.Cache;
import com.sparrow.core.Log;

public class Config {

	public static String getNewUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	public static String getValueInFile(String strPropertiesKey, String language) {
		if (StringUtil.isNullOrEmpty(language)) {
			language = getValueInCache(CONFIG.LANGUAGE);
		}
		Object propertiesValue = "";
		Properties props = new Properties();
		try {
			props.load(new FileInputStream(FileUtil.getInstance().getClassesPhysicPath()
					+ "/messages_" + language + ".properties"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		propertiesValue = props.getProperty(strPropertiesKey);
		try {
			propertiesValue = new String(propertiesValue.toString().getBytes(
					"ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return propertiesValue.toString();
	}

	public static String getValueInCache(String key) {
		try {
			Object value = Cache.getInstance().get(
					CACHE_KEY.CONFIG_FILE, key);
			if (value == null) {
				return null;
			} else {
				return value.toString();
			}
		} catch (Exception e) {
			Log.getInstance().error(e);
			return null;
		}
	}
}
